#include "clickedlabel.h"
#include "global.h"

#include <QMouseEvent>

ClickedLabel::ClickedLabel(QWidget *parent)
:QLabel(parent)
,_curstate(ClickLbState::Normal)
{
    // 设置鼠标样式为手形，表示可点击
    this->setCursor(Qt::PointingHandCursor);
}

void ClickedLabel::mousePressEvent(QMouseEvent* e)
{
    if (e->button() == Qt::LeftButton)
    {
        if(_curstate == ClickLbState::Normal)
        {
            _curstate = ClickLbState::Selected;
            setProperty("state",_selected_hover);
            repolish(this);
            update();
        }
        else
        {
            _curstate = ClickLbState::Normal;
            setProperty("state",_normal_hover);
            repolish(this);
            update();
        }
        emit clicked();
    }
    // 调用基类的mousePressEvent以保证正常的事件处理
    QLabel::mousePressEvent(e);
}

void ClickedLabel::enterEvent(QEnterEvent *event)
{
    if(_curstate == ClickLbState::Normal)
    {
        setProperty("state",_normal_hover);
        repolish(this);
        update();
    }
    else
    {
        setProperty("state",_selected_hover);
        repolish(this);
        update();
    }
    QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    if(_curstate == ClickLbState::Normal)
    {
        setProperty("state",_normal);
        repolish(this);
        update();
    }
    else
    {
        setProperty("state",_selected);
        repolish(this);
        update();
    }
    QLabel::leaveEvent(event);
}

void ClickedLabel::SetState(QString normal, QString hover, QString press,
              QString select, QString select_hover, QString select_press)
{
    _normal = normal;
    _normal_hover = hover;
    _normal_press = press;

    _selected = select;
    _selected_hover = select_hover;
    _selected_press = select_press;

    setProperty("state", normal);
    repolish(this);
}

ClickLbState ClickedLabel::GetCurState()
{
    return _curstate;
}
